﻿using System;
using System.Collections.Generic;

//Write a program that extracts from given HTML file its title 
//(if available), and its body text without the HTML tags

    class ExtractHtmlTitleAndBody
    {
        static string RemovingTags(string line)
        {
            List<int> indexOpenTag = new List<int>();
            List<int> indexCloseTag = new List<int>();
            int indexOpen = line.IndexOf('<', 0);
            indexOpenTag.Add(indexOpen);
            int indexClose = line.IndexOf('>', 0);
            indexCloseTag.Add(indexClose);

            while (indexClose != -1)
            {
                indexOpen = line.IndexOf('<', indexOpen + 1);
                indexOpenTag.Add(indexOpen);
                indexClose = line.IndexOf('>', indexClose + 1);
                indexCloseTag.Add(indexClose);
            }

            int difference = 0;
            int currDifference = 0;
            for (int i = 0; i < indexOpenTag.Count - 1; i++)
            {
                indexOpenTag[i] = indexOpenTag[i] - difference;
                indexCloseTag[i] = indexCloseTag[i] - difference;
                currDifference = ((int)indexCloseTag[i]) - ((int)indexOpenTag[i]) + 1;
                difference += currDifference;
                line = line.Remove(indexOpenTag[i], currDifference);
            }

            return line;

        }

        static void Main()
        {
            string htmltext = @"<html>
  <head><title>News</title></head>
  <body><p><a href=""http://academy.telerik.com"">Telerik
    Academy</a>aims to provide free real-world practical
    training for young people who want to turn into
    skillful .NET software engineers.</p></body>
</html>";
            int indexTitleOpen = htmltext.IndexOf("<title>",0);
            int indexTitleClose = htmltext.IndexOf("</title>", 0);
            if (indexTitleOpen>0)
            {
                Console.WriteLine(htmltext.Substring(indexTitleOpen+7,indexTitleClose-indexTitleOpen-7));
            }
            int indexBodyOpen = htmltext.IndexOf("<body>", 0);
            int indexBoodyClose = htmltext.IndexOf("</body>", 0);

            string bodyContent = htmltext.Substring(indexBodyOpen+6,indexBoodyClose-indexBodyOpen-6);
            Console.WriteLine(RemovingTags(bodyContent));
        }
    }

